package com.ford.trade.processor;

import com.alibaba.fastjson.JSONObject;
import com.ford.trade.entity.AoKeOdds;
import com.ford.trade.service.AoKeGameService;
import com.ford.trade.service.OddsService;
import lombok.Data;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.Date;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class AoKeOddsProcessor implements PageProcessor {

    private AoKeGameService gameService;
    private OddsService oddsService;

    private String type;
    private String date;
    private String cookie;
    private String agent;

    @Override
    public void process(Page page) {
        AoKeOdds aoKeOdds = new AoKeOdds();
        Html html = page.getHtml();
        String script = html.css("script").get();
        String static_str = script.substring(script.indexOf("static_str") + 14);
        String substring = static_str.substring(0, static_str.indexOf(";")-1);
        System.out.println(script);
        System.out.println(static_str);
        System.out.println(substring);
        JSONObject jsonObject = JSONObject.parseObject(substring);
        System.out.println(jsonObject);

        String url = page.getUrl().get();
        String type = "";

        Long indexId = Long.valueOf(url.substring(35, 35+url.substring(35).indexOf("/")));
        int length = url.length();
        if (url.contains("BaijiaBooks")) {
            type = "百家欧赔";
        }else if (url.contains("ExceptExchangeMakerID")){
            type = "非交易所";
        }else if (url.contains("ExchangeBooks")){
            type = "交易所";
        }else if (url.contains("AuthoriteBooks")){
            type = "权威博彩公司";
        }else if (url.contains("BigBooks")){
            type = "bigCompany";
        }else if (url.contains("SmallBooks")){
            type = "小公司";
        }else if (url.contains("LeagueBooks")){
            type = "赛事专用公司";
        }else if (url.contains("MyOddsBooks")){
            type = "定制公司";
        }else if (url.contains("MyOddsBooks2")){
            type = "威立韦";
        }else {
            type = "百家欧赔";
        }
        aoKeOdds.setIndexId(indexId);
        aoKeOdds.setType(type);
        JSONObject avg = jsonObject.getJSONObject("avg");
        //初始胜
        aoKeOdds.setSheng(Double.valueOf(avg.getJSONObject("Start").getString("home")));
        //初始平
        aoKeOdds.setPing(Double.valueOf(avg.getJSONObject("Start").getString("draw")));
        //初始负
        aoKeOdds.setFu(Double.valueOf(avg.getJSONObject("Start").getString("away")));
        //变化胜
        aoKeOdds.setShengChange(Double.valueOf(avg.getJSONObject("End").getString("home")));
        //变化平
        aoKeOdds.setPingChange(Double.valueOf(avg.getJSONObject("End").getString("draw")));
        //变化负
        aoKeOdds.setFuChange(Double.valueOf(avg.getJSONObject("End").getString("away")));
        //变化胜凯利
        aoKeOdds.setShengKellyChange(Double.valueOf(avg.getJSONObject("Kelly").getString("home")));
        //变化平凯利
        aoKeOdds.setPingKellyChange(Double.valueOf(avg.getJSONObject("Kelly").getString("draw")));
        //变化负凯利
        aoKeOdds.setFuKellyChange(Double.valueOf(avg.getJSONObject("Kelly").getString("away")));
        //返还率
        aoKeOdds.setReturnRate(avg.getString("Payoff"));
        JSONObject variance = jsonObject.getJSONObject("variance");
        //凯利方差
        aoKeOdds.setKVhome(variance.getJSONObject("KV").getString("home"));
        aoKeOdds.setKVdraw(variance.getJSONObject("KV").getString("draw"));
        aoKeOdds.setKVaway(variance.getJSONObject("KV").getString("away"));
        //凯利离散度
        aoKeOdds.setKDhome(variance.getJSONObject("KD").getString("home"));
        aoKeOdds.setKDdraw(variance.getJSONObject("KD").getString("draw"));
        aoKeOdds.setKDaway(variance.getJSONObject("KD").getString("away"));
        //澳客胜负指数
        aoKeOdds.setOEhome(variance.getJSONObject("OE").getString("home"));
        aoKeOdds.setOEdraw(variance.getJSONObject("OE").getString("draw"));
        aoKeOdds.setOEaway(variance.getJSONObject("OE").getString("away"));
        aoKeOdds.setDatetime(new Date());
        oddsService.save(aoKeOdds);
    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36")
            .setTimeOut(1000)
            .addCookie("cookie","LastUrl=; __utmc=56961525; __utmz=56961525.1653290495.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; BAIDU_SSP_lcr=https://www.baidu.com/link?url=SStKhMhziiQVseuA1E9Z3SpPEKPbhE5wJfvXcIirKfm&wd=&eqid=d207e11c000151cb00000004628b37a4; Hm_lvt_213d524a1d07274f17dfa17b79db318f=1652765523,1652862617,1652921906,1653290495; PHPSESSID=49140db8cad3f6d59ee8612b9bd82ddecc02b0ab; pm=; FirstURL=www.okooo.com/; FirstOKURL=https%3A//www.okooo.com/danchang/; First_Source=www.okooo.com; IMUserID=31747680; IMUserName=%E9%A3%9E%E9%B8%9F%E9%9B%B7%E9%B8%A3; OKSID=49140db8cad3f6d59ee8612b9bd82ddecc02b0ab; M_UserName=%22%5Cu98de%5Cu9e1f%5Cu96f7%5Cu9e23%22; M_UserID=31747680; M_Ukey=39755d37eb1ef335ded3a5b5f523199c; OkAutoUuid=a8a9a29ce8bbd1db3b12c2d1c00b91e0; OkMsIndex=5; data_start_isShow=1; __utma=56961525.267670634.1653290495.1653291754.1653380750.4; acw_tc=2f624a1e16533811782431430e4610a1068f8c3547ccdb741f4d1cce659d9e; Hm_lpvt_213d524a1d07274f17dfa17b79db318f=1653381360; __utmb=56961525.40.8.1653381361038")
            .setSleepTime(3)
            .setRetrySleepTime(1000)
            .setRetryTimes(1);

    @Override
    public Site getSite() {
        return site;
    }

}
